﻿// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Copyright (c) 2011, Dr. Masroor Ehsan
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the author nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// $Author:$
// $Id:$
// $Rev:$
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

using System.Collections.Generic;
using MaxTrader.Core;

namespace MaxTrader.BusinessObjects
{
    /// <summary>
    ///   Represents position (holding status) of an individual stock
    /// </summary>
    public sealed class Position : Entity
    {
        private readonly List<ShareTransaction> _transactions;

        public Position(Portfolio portfolio, StockSymbol symbol, TradingDate date, int shares, decimal price,
                        decimal commission,
                        decimal cost)
        {
            _transactions = new List<ShareTransaction>();

            Portfolio = portfolio;
            Symbol = symbol;
            Side = PositionSide.Flat;
            OpenDate = date;
            NumberOfShares = shares;
            AveragePrice = price;
            TotalCommission = commission;
            AverageCost = cost;
        }

        public Portfolio Portfolio { get; set; }

        /// <summary>
        ///   Current side
        /// </summary>
        public PositionSide Side { get; set; }

        /// <summary>
        ///   The position side on entry
        /// </summary>
        public PositionSide EntrySide { get; set; }

        /// <summary>
        ///   Stock held
        /// </summary>
        public StockSymbol Symbol { get; set; }

        /// <summary>
        ///   Date shares were purchased
        /// </summary>
        public TradingDate OpenDate { get; set; }

        /// <summary>
        ///   Date this position was closed
        /// </summary>
        public TradingDate CloseDate { get; set; }

        /// <summary>
        ///   Number of shares of stock
        /// </summary>
        public int NumberOfShares { get; set; }

        /// <summary>
        ///   Average price per share
        /// </summary>
        public Money AveragePrice { get; set; }

        /// <summary>
        ///   Brokerage commission
        /// </summary>
        public Money TotalCommission { get; set; }

        /// <summary>
        ///   Average cost of shares (avg price + avg commission)
        /// </summary>
        public Money AverageCost { get; private set; }

        public List<ShareTransaction> Transactions
        {
            get { return _transactions; }
        }

        /// <summary>
        ///   Increase ownership of stock.
        /// </summary>
        /// <param name = "shares">number of new shares to accumulate</param>
        /// <param name = "price">average price of shares</param>
        /// <param name = "commission">brokerage commission</param>
        public void Accumulate(int shares, decimal price, decimal commission)
        {
            AveragePrice = ((NumberOfShares*AveragePrice) + (shares*price))/(NumberOfShares + shares);
            NumberOfShares += shares;
            TotalCommission = TotalCommission + commission;
            AverageCost = AveragePrice + (TotalCommission/NumberOfShares);
        }

        /// <summary>
        ///   Decrease ownership of stock.
        /// </summary>
        /// <param name = "shares">number of shares to reduce</param>
        /// ///
        /// <param name = "commission">brokerage commission</param>
        public void Reduce(int shares, decimal commission)
        {
            NumberOfShares -= shares;
            TotalCommission = TotalCommission + commission;
        }
    }
}